package com.mercury.base.result;

import com.mercury.base.result.code.BaseCode;
import com.mercury.base.result.code.IRespCode;
import lombok.EqualsAndHashCode;
import lombok.ToString;

import java.beans.ConstructorProperties;
import java.io.Serializable;
import java.util.Collections;
import java.util.List;

/**
 * list集合
 *
 * @author mercury
 * @date 2020/10/11 2:59 下午
 */
@EqualsAndHashCode(callSuper = true)
@ToString(callSuper = true)
public class ResultListVo<E> extends BaseResultCoreResult<List<E>> implements Serializable {

    private static final long serialVersionUID = 8960474786737581150L;

    public static <E> ResultListVo<E> ok() {
        return ok(Collections.emptyList());
    }

    public static <E> ResultListVo<E> ok(List<E> data) {
        return build(BaseCode.SUCCESS.getRespCode(), BaseCode.SUCCESS.getMessage(), data);
    }

    public static <E> ResultListVo<E> err(IRespCode code) {
        return err(code, code.getMessage());
    }

    public static <E> ResultListVo<E> err(IRespCode code, String message) {
        return err(code.getRespCode(), message);
    }

    public static <E> ResultListVo<E> err(String message) {
        return err(BaseCode.SERVER_ERROR.getRespCode(), message);
    }

    public static <E> ResultListVo<E> err(IRespCode code, List<E> data) {
        return build(code.getRespCode(), code.getMessage(), data);
    }

    public static <E> ResultListVo<E> err(String code, String message) {
        if (code == null) {
            return err(message);
        }
        return build(code, message, null);
    }

    public static <E> ResultListVo<E> build(IRespCode code) {
        return build(code, null);
    }

    public static <E> ResultListVo<E> build(IRespCode code, List<E> data) {
        return build(code.getRespCode(), code.getMessage(), data);
    }

    public static <E> ResultListVo<E> build(String code, String message, List<E> data) {
        return new ResultListVo<>(code, message, data);
    }

    @ConstructorProperties({"retcode", "message", "data"})
    public ResultListVo(String code, String message, List<E> data) {
        super(code, message, data);
    }

}